package 力扣._530_二叉搜索树的最小绝对差;

import 力扣.公用数据结构.TreeNode;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 * int val;
 * TreeNode left;
 * TreeNode right;
 * TreeNode() {}
 * TreeNode(int val) { this.val = val; }
 * TreeNode(int val, TreeNode left, TreeNode right) {
 * this.val = val;
 * this.left = left;
 * this.right = right;
 * }
 * }
 */
class Solution {

    private int min = Integer.MAX_VALUE;
    private int last = -1;

    private void mid(TreeNode root) {
        if (root.left != null)
            mid(root.left);
        if(last!=-1) {
            min = Math.min(Math.abs(last - root.val), min);
        }
        last = root.val;

        if (root.right != null)
            mid(root.right);
    }

    public int getMinimumDifference(TreeNode root) {
        // 中序遍历即可
        mid(root);
        return min;
    }

    public static void main(String[] args) {
        TreeNode node = TreeNode.creat("[236,104,701,null,227,null,911]");
        int minimumDifference = new Solution().getMinimumDifference(node);
        System.out.println(minimumDifference);
    }
}